package service

import (
	"encoding/json"
	"fmt"

	"im/dao"
	"im/model"
	"im/requests"
	"im/responses"
	"im/service/push"
)

type InternalMails interface {
	// 获取列表数据
	List(userId int64, param requests.GetInternalMails) (responses.GetInternalMails, error)
	// 新建站内信
	Create(row model.InternalMails) error
	// 清空所有已读站内信
	HideAllRead(userId int64) error

	// 已读回执
	ReadReceipt(userId int64, mailIdList ...int64) error
	// 全部已读
	ReadAllUnread(userId int64) error
	AddCommentMails(postId int64, comment string, fromUser model.Users) error
	AddReplyMails(postId int64, replyId int64, reply string, fromUser model.Users) error
}

func NewInternalMails() InternalMails {
	return internalMails{}
}

type internalMails struct {
}

func (i internalMails) AddReplyMails(postId int64, replyId int64, content string, fromUser model.Users) error {
	reply, err := dao.NewReplys().GetById(replyId)
	if err != nil {
		return err
	}
	row := model.InternalMails{
		AutoId:      model.AutoId{},
		TimeModel:   model.TimeModel{},
		UId:         model.UId{UserId: reply.UserId},
		Title:       fmt.Sprintf("%s回复了你", fromUser.Nickname),
		Content:     content,
		ContentType: model.InternalMail_ContentType_TEXT,
		RedirectUrl: "",
		ContentOptions: i.genenOptions(map[string]interface{}{
			"post_id":  postId,
			"reply_id": replyId,
		}),
		ReadStatus:   1,
		HiddenStatus: 1,
	}
	return i.Create(row)
}
func (i internalMails) genenOptions(data map[string]interface{}) string {
	b, _ := json.Marshal(data)
	return string(b)
}

func (i internalMails) AddCommentMails(postId int64, comment string, fromUser model.Users) error {
	post, err := NewPosts().GetById(0, postId)
	if err != nil {
		return err
	}
	row := model.InternalMails{
		AutoId:      model.AutoId{},
		TimeModel:   model.TimeModel{},
		UId:         model.UId{UserId: post.UserId},
		Title:       fmt.Sprintf("%s评论了你的帖子", fromUser.Nickname),
		Content:     comment,
		ContentType: model.InternalMail_ContentType_TEXT,
		RedirectUrl: "",
		ContentOptions: i.genenOptions(map[string]interface{}{
			"post_id": postId,
		}),
		ReadStatus:   1,
		HiddenStatus: 1,
	}
	return i.Create(row)
}

func (i internalMails) List(userId int64, param requests.GetInternalMails) (responses.GetInternalMails, error) {
	var (
		res = responses.GetInternalMails{
			Page: param.Page,
		}
		err error
	)
	list, count, err := dao.NewInternalMailsDao().ListBy(userId, param)
	if err != nil {
		return res, err
	}
	res.List, res.Count = list, count
	return res, nil
}

func (i internalMails) Create(row model.InternalMails) error {
	_, err := dao.NewInternalMailsDao().Insert(row)
	if err != nil {
		return err
	}
	userInfo := dao.NewUsersDao().Info(row.UserId)
	err = push.PushMessage(userInfo.PushToken, row.Title)
	return nil
}

func (i internalMails) HideAllRead(userId int64) error {
	err := dao.NewInternalMailsDao().HideAllRead(userId)
	if err != nil {
		return err
	}
	return nil
}

func (i internalMails) ReadReceipt(userId int64, mailIdList ...int64) error {
	err := dao.NewInternalMailsDao().ReadReceipt(userId, mailIdList)
	if err != nil {
		return err
	}
	return nil
}

func (i internalMails) ReadAllUnread(userId int64) error {
	err := dao.NewInternalMailsDao().ReadAllUnread(userId)
	if err != nil {
		return err
	}
	return nil
}
